package com.spzx.channel.configure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * ClassName: ThreadPoolConfig
 * Package: com.spzx.channel.configure
 * Description:
 *
 * @Author ycy
 * @Create 2024/8/19 18:55
 * @Version 1.0
 */
@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        int processorsCount = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                processorsCount * 2,
                processorsCount * 2, 0,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(200),
                Executors.defaultThreadFactory(),
                // new ThreadPoolExecutor.CallerRunsPolicy()
                // 自定义拒绝策略
                (runnable, executor) -> {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        //再次将拒绝任务交给线程池处理
                        executor.submit(runnable);
                    }
                }
        );
        //线程池创建，核心线程同时创建
        // threadPoolExecutor.preStartCoreThread();
        threadPoolExecutor.prestartAllCoreThreads();
        return threadPoolExecutor;
    }

}
